package com.itheima.info.system.manager.controller;

import com.itheima.info.system.manager.domain.Teacher;
import com.itheima.info.system.manager.factory.Factory;
import com.itheima.info.system.manager.service.IBaseTeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherController implements IBaseTeacherController {
    private Scanner sc = new Scanner(System.in);
IBaseTeacherService teacherService = Factory.getTeacherService();    
    @Override
    public void start() {
        while (true) {
            System.out.println("---------------------欢迎来到<教师>管理系统--------------------");
            System.out.println("\t1.添加教师\t2.删除教师\t3.修改教师\t4.查看教师\t5.退出系统");
            System.out.print("\t您的选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    add();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    updata();
                    break;
                case 4:
                    findAll();
                    break;
                case 5:
                    System.out.println("\t感谢使用");
                    return;
                default:
                    System.out.print("\t您的输入有误,请重新输入:");
                    break;
            }
        }
    }

    @Override
    public void add() {
        String sid;
        while (true) {
            System.out.print("\t请输入工号:");
            sid = sc.next();
            boolean exist = teacherService.isExist(sid);
            if (exist){
                System.out.println("\t学号已存在,请重新输入.");
            }else{
                break;
            }
        }

        Teacher teacher = getInfoteacher(sid);
        teacherService.add(teacher);
        System.out.println("\t添加成功");
    }

    @Override
    public Teacher getInfoteacher(String sid) {
        System.out.print("\t请输入姓名:");
        String name = sc.next();
        System.out.print("\t请输入年龄:");
        int age = sc.nextInt();
        System.out.print("\t请输入班级:");
        String cls = sc.next();
        return new Teacher(name,sid,age,cls);
    }
    @Override
    public void delete() {
       if (teacherService.isEmpty()){
           System.out.println("\t还未添加数据");
       }else {
           String sid = getSid();
           teacherService.delete(sid);
           System.out.println("\t删除成功");
       }
    }

    @Override
    public String getSid() {
        String sid;
        while (true) {
            System.out.print("\t请输入工号:");
            sid = sc.next();
            boolean exist = teacherService.isExist(sid);
            if (exist) {
                break;
            } else {
                System.out.println("\t工号不存在,请重新输入.");
            }
        }
        return sid;
    }

    @Override
    public void updata() {
        if (teacherService.isEmpty()){
            System.out.println("\t还未添加数据");
        }else {
            String sid = getSid();
            Teacher teacher = getInfoteacher(sid);
            teacherService.updata(sid, teacher);
            System.out.println("\t修改成功");
        }
    }

    @Override
    public void findAll() {
        if (teacherService.isEmpty()) {
            System.out.println("\t您还未录入数据,请先录入.");
        } else {
            ArrayList<Teacher> teachers = teacherService.findAll();
            System.out.println("\t学号 \t姓名 \t年龄 \t班级");
            for (Teacher teacher : teachers) {
                System.out.println(teacher.getSid() + "\t" + teacher.getName() + "\t"
                        + teacher.getAge() + "\t" + teacher.getCls() + "\t");
            }
        }
    }

}
